/**
 * 
 */
package ender.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;


/**
 * @author ender
 *
 */
public final class StringUtil {

	public static boolean isEmpty(String str){
		return str == null || str.trim().length()==0;
	}
	
	public static boolean isNumStr(String str){
		return !isEmpty(str) && Pattern.matches("^[-+]?\\d+(\\.\\d+)?$", str);
	}
	
	
	public static String genErrResult(int errCode, String errMsg){
		return "{errCode:"+errCode+", errMsg:"+errMsg+"}";
	}
	
	public static String genRichfitResp(boolean success, int errCode, String errMsg, String resultData){
		JSONObject json = new JSONObject();
		json.put("success", success);
		json.put("err_code", errCode);
		json.put("err_msg", isEmpty(errMsg)?"":errMsg);
		json.put("result_data", isEmpty(resultData)?"":JSONObject.parseObject(resultData));
		return json.toJSONString();
	}
	
	public static boolean isTrue(String boolStr){
		return (!isEmpty(boolStr) && (boolStr.equalsIgnoreCase("TRUE") || boolStr.equalsIgnoreCase("T") || boolStr.equalsIgnoreCase("0")))?true:false;
	}
	
	
	public static String toUnderLineStr(String str){
		Pattern p = Pattern.compile("[A-Z]");
		Matcher m = p.matcher(str);
		return m.replaceAll("_$0").toLowerCase();
	}
	
	public static boolean isIp(String str){
	    return !isEmpty(str) && Pattern.matches("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", str);
	}
	
	public static boolean isPort(String port){
	    return !isEmpty(port) && Pattern.matches("^[-+]?\\d+(\\.\\d+)?$", port) && Integer.parseInt(port)>=0 && Integer.parseInt(port)<=65535;    
	}
	
	public static String toHex(int value){
	    return Integer.toHexString(value);
	}
	
	public static <E> String getCollectionsStr(Collection<E> c, String splitStr){
	    if(c.size() == 0){
	        return "";
	    }
	    StringBuilder strBuilder = new StringBuilder();
	    for(E e : c){
	        strBuilder.append(e.toString());
	        strBuilder.append(splitStr);
	    }
	    return isEmpty(splitStr)?strBuilder.toString():strBuilder.delete(strBuilder.lastIndexOf(splitStr), strBuilder.length()).toString();
	}
	
	   public static <E> String getCollectionsStr(Object source, Collection<E> c, Method m, String splitStr) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
	       if(c.size() == 0){
	            return "";
	        }
	       m.setAccessible(true); 
	       StringBuilder strBuilder = new StringBuilder();
	        for(E e : c){
	            strBuilder.append(m.invoke(source, e));
	            strBuilder.append(splitStr);
	        }
	        return isEmpty(splitStr)?strBuilder.toString():strBuilder.delete(strBuilder.lastIndexOf(splitStr), strBuilder.length()).toString();
	    }
	   
	   public static <T,E> String getMapStr(Object source, Map<T, E> map, Method m, String splitStr) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
           m.setAccessible(true); 
           return m.invoke(source, map, splitStr).toString();
        }
	
	public static void main(String[] args){
//		System.out.println(isNumStr("-0.a0001"));
//		System.out.println(isTrue("t"));
//		JSONObject json = new JSONObject();
//		json.put("1", 1);
//		json.put("String", "String");
//		System.out.println(genResp(true, 0, null, json.toJSONString()));
//		System.out.println(genResp(true, 0, null, null));
//		System.out.println(toUnderLineStr("expireTime"));
	    
//	    System.out.println(isIp("255.11.11.1"));
//	    System.out.println(isPort("65536"));
	    
	}
	
}
